/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoggedInJPanel.java
 *
 * Created on Jan 15, 2010, 12:58:50 AM
 */

package viper.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;

import viper.main.MySQLFunctions;
import viper.main.StateManager;

/**
 *
 * @author Kevin Sin
 * 
 * Revision number: 1
 * Date created: 1/17/10
 * Date of Last Back-up on SVN: none
 * 
 * <p>JPanel for when user first logs in.  Displays the peers online.
 */
public class LoggedInPanel extends javax.swing.JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form LoggedInJPanel */
    public LoggedInPanel() {
        initComponents();
    }
    
    private javax.swing.JButton refreshButton; //Refresh button
    private javax.swing.JButton logoutButton; //Logout button
    private javax.swing.JButton nextButton; //Next button
    private javax.swing.JLabel jLabel1; //Directions
    private javax.swing.JList jList1; //Displaying peers
    private javax.swing.JScrollPane jScrollPane1; //For peers list
    private  DefaultListModel listModel; //Also for peers list

    /** This method is called from within the constructor to
     * initialize the form.
     */
    private void initComponents() {

    	setLayout(new BorderLayout());
    	
    	refreshButton = new javax.swing.JButton();
    	
        logoutButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();

        jLabel1 = new javax.swing.JLabel();
        nextButton = new javax.swing.JButton();
        
        refreshButton.setText("Refresh");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        logoutButton.setText("Log out");
        logoutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutButtonActionPerformed(evt);
            }
        });
        
        jLabel1.setText("Choose who you want to call:");
        
        listModel = new DefaultListModel();
    	jList1 = new JList(listModel);
    	jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jList1);
        jList1.setCellRenderer(new IconListRenderer());

        nextButton.setText("Next >");
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });
        
        //Refresh and logout
        JPanel buttonPane = new JPanel();
		buttonPane.setBackground(Color.WHITE);
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
        buttonPane.add(Box.createHorizontalGlue());
        buttonPane.add(refreshButton);
        buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonPane.add(logoutButton, BorderLayout.LINE_END);
        
        //scroll panel
        JPanel scrollPanel = new JPanel();
        scrollPanel.setBackground(Color.WHITE);
        scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.LINE_AXIS));
        scrollPanel.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));
        scrollPanel.add(jScrollPane1, BorderLayout.CENTER);
        
        //next button
        JPanel nextPane = new JPanel();
        nextPane.setBackground(Color.WHITE);
        nextPane.setLayout(new BoxLayout(nextPane, BoxLayout.LINE_AXIS));
        nextPane.add(Box.createHorizontalGlue());
        nextPane.add(nextButton, BorderLayout.LINE_END);
        
        
        add(buttonPane, BorderLayout.NORTH);
        add(scrollPanel, BorderLayout.CENTER);
        add(nextPane, BorderLayout.AFTER_LAST_LINE);
    }

    /**
     * @param evt
     * Logout button pressed
     */
    private void logoutButtonActionPerformed(java.awt.event.ActionEvent evt) {
        StateManager.INSTANCE.logout();
    }

    /**
     * @param evt
     * Refresh button pressed
     */
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	StateManager.INSTANCE.updatePeers();
    	update();
    }
    
    /**
     * @param evt
     * Next button pressed
     */
    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	//int index = jList1.getSelectedIndex();
    	JLabel selectedLabel = (JLabel)jList1.getSelectedValue();
    	String peerString = selectedLabel.getText();
    	if(peerString!=null)
    	{
    		String peerIP = peerString.split("---")[1];
    		StateManager.INSTANCE.setupCall(peerIP);
    	}
//    	if(index!=-1)
//    		StateManager.INSTANCE.setupCall(index);
    }

    /**
	 * Updates the peers to display.
	 */
	public void update(){
		ImageIcon icon = null;
		listModel.clear();
		for(Object[] peer : StateManager.INSTANCE.getPeers())
		{
			String peerString = peer[1] + "---" + peer[0];
			if(peer[2].equals(MySQLFunctions.STATUS_FREE))
			{
				icon = new ImageIcon("images/green_phone.gif");
			}
			else if(peer[2].equals(MySQLFunctions.STATUS_BUSY))
			{
				icon = new ImageIcon("images/busy.gif");
			}
			else if(peer[2].equals(MySQLFunctions.STATUS_BAD_RELAY))
			{
				icon = new ImageIcon("images/relay.gif");
			}
			//listModel.addElement(peerString);
			JLabel label = new JLabel(peerString);
			label.setIcon(icon);
			listModel.addElement(label);
		}
		jList1.repaint();
	}
    
}
